<template>
  <div>
    <div>
      <DocDetHead @showStructureAnnotation="toggleStructureAnnotationModal"
        @showEntityAnnotation="toggleEntityAnnotationModal" @showRelationAnnotation="toggleRelationAnnotationModal"
        @showKnowledgeGraphView="toggleKnowleGraphViewModal" @showExportData="toggleExportDataModal" />
    </div>
    <div class="main">
      <div v-if="currentModal === 'structure'">
        <StructureAnnotation />
      </div>
      <div v-if="currentModal === 'entity'">
        <EntityAnnotation />
      </div>
      <div v-if="currentModal === 'relation'">
        <RelationAnnotation />
      </div>
      <div v-if="currentModal === 'knowledge'">
        <KnowledgeGraphView />
      </div>
      <div v-if="currentModal === 'export'">
        <ExportData />
      </div>
      <div v-if="currentModal === null">
        <StructureAnnotation />
      </div>
    </div>
  </div>
</template>
<script setup>
import DocDetHead from '@/views/demension/DocDetHead.vue'
import StructureAnnotation from '@/views/demension/StructureAnnotation.vue'
import EntityAnnotation from '@/views/demension/EntityAnnotation.vue'
import RelationAnnotation from '@/views/demension/RelationAnnotation.vue'
import KnowledgeGraphView from '@/views/demension/KnowledgeGraphView.vue'
import ExportData from '@/views/demension/ExportData.vue'
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { useRoute } from 'vue-router'
import axios from '@/plugins/axios'
import { ElMessage } from 'element-plus'
import { useEditorStore } from '@/store/editor'
import { useProjectStore } from '@/store/project'

const route = useRoute()
const editorStore = useEditorStore()
const projectStore = useProjectStore()

// 获取URL中的项目ID和项目名称
const projectId = computed(() => route.params.projectId)
console.log('项目ID：', projectId.value)
const projectName = computed(() => route.params.projectName)
console.log('项目名称：', projectName.value)
const projectDescription = computed(() => route.params.projectDescription)

const showStructureAnnotationModal = ref(false)
const showEntityAnnotationModal = ref(false)
const showRelationAnnotationModal = ref(false)
const showKnowleGraphViewModal = ref(false)
const showExportDataModal = ref(false)

const currentModal = computed(() => {
  if (showStructureAnnotationModal.value) return 'structure'
  if (showEntityAnnotationModal.value) return 'entity'
  if (showRelationAnnotationModal.value) return 'relation'
  if (showKnowleGraphViewModal.value) return 'knowledge'
  if (showExportDataModal.value) return 'export'
  return null
})

const toggleStructureAnnotationModal = () => {
  showStructureAnnotationModal.value = true
  showEntityAnnotationModal.value = false
  showRelationAnnotationModal.value = false
  showKnowleGraphViewModal.value = false
  showExportDataModal.value = false
}

const toggleEntityAnnotationModal = () => {
  showEntityAnnotationModal.value = true
  showStructureAnnotationModal.value = false
  showRelationAnnotationModal.value = false
  showKnowleGraphViewModal.value = false
  showExportDataModal.value = false
}

const toggleRelationAnnotationModal = () => {
  showRelationAnnotationModal.value = true
  showStructureAnnotationModal.value = false
  showEntityAnnotationModal.value = false
  showKnowleGraphViewModal.value = false
  showExportDataModal.value = false
}

const toggleKnowleGraphViewModal = () => {
  showKnowleGraphViewModal.value = true
  showStructureAnnotationModal.value = false
  showEntityAnnotationModal.value = false
  showRelationAnnotationModal.value = false
  showExportDataModal.value = false
}
const toggleExportDataModal = () => {
  showExportDataModal.value = true
  showStructureAnnotationModal.value = false
  showEntityAnnotationModal.value = false
  showRelationAnnotationModal.value = false
  showKnowleGraphViewModal.value = false
}

// 获取文档数据的方法
const fetchDocument = async () => {
  const documentId = route.params.documentId

  if (!documentId) {
    ElMessage.error('未找到文档ID，无法获取文档信息')
    return
  }

  try {
    const response = await axios.post('/get_document', { documentId })

    if (response.data && response.data.code === 200) {
      console.log('文档数据获取成功:', response.data.data.document)
      const document = response.data.data.document

      // 将文档数据存储到store中
      editorStore.setDocumentInfo(document)

      // 同步项目信息
      projectStore.syncProjectInfo({
        projectId: projectId.value,
        projectName: projectName.value,
        projectDescription: projectDescription.value
      })

      // 设置编辑器内容
      if (document.content) {
        editorStore.setContent(document.content)
      }

      console.log('文档数据获取成功:', response.data.data)
    } else {
      ElMessage.error('获取文档数据失败: ' + (response.data.message || '未知错误'))
    }
  } catch (error) {
    console.error('获取文档数据出错:', error)
    ElMessage.error('获取文档数据请求失败，请检查网络连接')
  }
}

// 组件挂载时获取文档数据
onMounted(() => {
  // 首先同步项目信息
  projectStore.syncProjectInfo({
    projectId: projectId.value,
    projectName: projectName.value,
    projectDescription: projectDescription.value
  })
  // 然后获取文档数据
  fetchDocument()
})

// 组件卸载前清理store
onBeforeUnmount(() => {
  // 重置编辑器状态，清除文档信息和内容
  editorStore.resetEditor()
  projectStore.clearProjectInfo()
})
</script>
<style>
.main {
  background-color: rgb(239, 239, 239);
  margin-top: 50px;
}
</style>
